/*
 * This file is part of Dorado 7.x (http://dorado7.bsdn.org).
 * 
 * Copyright (c) 2002-2012 BSTEK Corp. All rights reserved.
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * and BSDN commercial (http://www.bsdn.org/licenses) licenses.
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at http://www.bstek.com/contact.
 */

package com.bstek.dorado.config.definition;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bstek.dorado.core.bean.Scope;
import com.bstek.dorado.core.resource.ResourceManager;
import com.bstek.dorado.core.resource.ResourceManagerUtils;
import com.bstek.dorado.data.config.definition.DataProviderDefinition;
import com.bstek.dorado.data.config.definition.DataProviderDefinitionManager;
import com.bstek.dorado.data.config.definition.DataResolverDefinition;
import com.bstek.dorado.data.config.definition.DataResolverDefinitionManager;

/**
 * 利用名称指向某配置声明对象的引用。
 * 
 * @author Benny Bao (mailto:benny.bao@bstek.com)
 * @since Mar 7, 2008
 */
public class DefaultDefinitionReference<T extends Definition> implements
		DefinitionReference<T> {
	private static final ResourceManager resourceManager = ResourceManagerUtils
			.get(DefaultDefinitionReference.class);


	private DefinitionManager<T> definitionManager;
	private String name;

	private static final String DEFAULT_DATA_PROVIDER = "com.bstek.dorado.data.provider.DirectDataProvider";
	private static final String DEFAULT_DATA_RESOLVER = "com.bstek.dorado.data.resolver.DirectDataResolver";
	private static final Scope DEFAULT_DATA_PROVIDER_SCOPE = null;
	private static final Scope DEFAULT_DATA_RESOLVER_SCOPE = null;
	private static final String BEAN_NAME_PREFIX = "spring:";
	private Class<T> generictype;
	private Log logger = LogFactory.getLog(this.getClass());
	/**
	 * @param definitionManager
	 *            配置声明管理器
	 * @param name
	 *            指向的声明对象在注册时使用的名称
	 */
	public DefaultDefinitionReference(DefinitionManager<T> definitionManager,
			String name) {
		this.definitionManager = definitionManager;
		this.name = name;
		
	}

	public DefinitionManager<T> getDefinitionManager() {
		return definitionManager;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public T getDefinition() {
		T definition = definitionManager.getDefinition(name);
		if (definition == null) {
			String[] arr = name.split("#");
			if(arr.length==2){
				DefinitionManager<T> dfmgr = this.getDefinitionManager();
				if(dfmgr instanceof DataProviderDefinitionManager){
					logger.warn("重新注册DataProvider:"+name);
					DataProviderDefinition def=new DataProviderDefinition(); 
					def.setName(name);
					def.setScope(DEFAULT_DATA_PROVIDER_SCOPE);
					try {
						def.setImpl(DEFAULT_DATA_PROVIDER);
					} catch (ClassNotFoundException e) {
						e.printStackTrace();
					}
					def.setInterceptor(BEAN_NAME_PREFIX + name);
					dfmgr.registerDefinition(name, (T)def);
					return (T) def;
				}else if(dfmgr instanceof DataResolverDefinitionManager){
					logger.warn("重新注册DataResolver:"+name);
					DataResolverDefinition def = new DataResolverDefinition();
					def.setName(name);
					def.setScope(DEFAULT_DATA_RESOLVER_SCOPE);
					try {
						def.setImpl(DEFAULT_DATA_RESOLVER);
					} catch (ClassNotFoundException e) {
						e.printStackTrace();
					}
					def.setInterceptor(BEAN_NAME_PREFIX + name);
					dfmgr.registerDefinition(name, (T)def);
					return (T) def;

				}
				logger.error("重新扫描后还是无法找到名为["+name+"]的definition");
				throw new IllegalArgumentException(resourceManager.getString(
						"dorado.common/unknownDefinition", name));	
			}else{
				logger.error("重新扫描后还是无法找到名为["+name+"]的definition");
				throw new IllegalArgumentException(resourceManager.getString(
						"dorado.common/unknownDefinition", name));
			}

			//执行重新注册Definition.如何重新注册？
		}
		
		return definition;
	}

}
